﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.DataModel
{
	public class RecordSystemElements
	{
		public RecordSystemElements(string projectName)
		{
			this.fileName = projectName;
			this.fileFullName = Path.Combine(Product.InstallLocation, "UserWork", this.fileName + this.suffix);
			this.LoadFile();
		}

		private void LoadFile()
		{
			if (File.Exists(this.fileFullName))
			{
				this.xmlDoc = new XmlDocument();
				this.xmlDoc.Load(this.fileFullName);
				return;
			}
			File.Copy(this.baseFileName, this.fileFullName);
			this.xmlDoc = new XmlDocument();
			this.xmlDoc.Load(this.fileFullName);
		}

		public List<string> GetAllRecordInfo(string sysName)
		{
			List<string> list = new List<string>();
			try
			{
				foreach (object obj in this.xmlDoc.SelectSingleNode("Systems").ChildNodes)
				{
					XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
					string attribute = xmlElement.GetAttribute("system");
					if (sysName.Equals(attribute))
					{
						list.Add(xmlElement.GetAttribute("elemID"));
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
			if (list.Count > 0)
			{
				return list;
			}
			return null;
		}

		public void WriteElementsID(string sysName, List<string> elemIDSet)
		{
			try
			{
				XmlNode xmlNode = this.xmlDoc.SelectSingleNode("Systems");
				foreach (string value in elemIDSet)
				{
					XmlElement xmlElement = this.xmlDoc.CreateElement("Element");
					xmlElement.SetAttribute("system", sysName);
					xmlElement.SetAttribute("elemID", value);
					xmlNode.AppendChild(xmlElement);
				}
				this.xmlDoc.Save(this.fileFullName);
			}
			catch (Exception)
			{
				throw;
			}
		}

		private string fileName = "";

		private string baseFileName = Product.InstallLocation + "\\Data\\HYRecordSystemElement.xml";

		private string fileFullName = "";

		private string suffix = "_RecordSE.xml";

		private XmlDocument xmlDoc;
	}
}
